package com.powernode.controller;

import com.powernode.domain.SysMenu;
import com.powernode.model.Result;
import com.powernode.service.SysMenuService;
import com.powernode.util.AuthUtil;
import com.powernode.vo.MenuAndAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/1/4 23:15
 */
@RestController
@Api(tags = "系统权限接口管理")
@RequestMapping("sys/menu")
@Slf4j
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询用户菜单权限和操作权限
     * @return
     */
    @GetMapping("nav")
    @ApiOperation("查询用户菜单权限和操作权限")
    public Result<MenuAndAuth>  getNav(){
        // 获取当前用户信息
        Long loginUserId = AuthUtil.getLoginUserId();

        // 获取当前用户所有操作权限信息
        Set<String> loginUserPerms = AuthUtil.getLoginUserPerms();

        // 获取当前用户所有菜单集合
        Set<SysMenu> sysMenus = sysMenuService.getUserMenuListByUserId(loginUserId);

        MenuAndAuth menuAndAuth = new MenuAndAuth(sysMenus,loginUserPerms);
        return Result.success(menuAndAuth);
    }

    /**
     * 查询所有菜单列表
     * @return
     */
    @ApiOperation("查询所有菜单列表")
    @GetMapping("table")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<SysMenu>> loadMenuList(){
        return Result.success(sysMenuService.queryMenuList());
    }


    /**
     * 新增菜单信息
     * @param sysMenu
     * @return
     */
    @ApiOperation("新增菜单信息")
    @PostMapping()
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result<String> saveMenu(@RequestBody SysMenu sysMenu){
        Integer count = sysMenuService.saveMenu(sysMenu);
        return Result.handle(count == 1);
    }


    /**
     * 根据menuId查询菜单详情
     * @return
     */
    @ApiOperation("根据menuId查询菜单详情")
    @GetMapping("info/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public Result<SysMenu> getMenuInfoByMenuId(@PathVariable(value = "menuId") Long menuId){
        SysMenu sysMenu = sysMenuService.queryMenuById(menuId);

        return Result.success(sysMenu);
    }

    /**
     * 修改菜单信息
     * @return
     */
    @ApiOperation("修改菜单信息")
    @PutMapping()
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result<String> updateMenu(@RequestBody SysMenu sysMenu){
        Integer count = sysMenuService.updateMenuById(sysMenu);
        return Result.handle(count == 1);
    }

    /**
     * 删除菜单信息
     * @return
     */
    @ApiOperation("删除菜单信息")
    @DeleteMapping("{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result<String> deleteMenu(@PathVariable(value = "menuId") Long menuId){

        Integer count = sysMenuService.deleteMenuById(menuId);
        return Result.handle(count == 1);
    }


}
